{extend name="common:base" /}
{block name="page_current"}
<div class="fui-page-group statusbar">
    <style>
        .fui-cell-group:not(.fui-cell-group-o):before{
            border:0
        }
        .fui-cell-group:first-child{
            margin-top: 0;
        }
    </style>
    <div class="fui-page  fui-page-current">
        <div class="fui-header">
            <div class="fui-header-left">
                <a class="back"></a>
            </div>
            <div class="title">{if(empty($isModfiy))}新建地址{else /}编辑地址{/if}</div>
            {if(!empty($isModfiy) && $model['id'] > 0)}
            <div class="fui-header-right"><a data-toggle="delete" data-addressid="{model:safe_show field='id' /}">删除</a></div>
            {/if}
        </div>
        <div class="fui-content">
            <form method="post" class="form-ajax">
                <input type="hidden" id="addressid" value="{model:safe_show field='id' /}">
                <div class="fui-cell-group">
                    <div class="fui-cell">
                        <div class="fui-cell-label">收货人</div>
                        <div class="fui-cell-info c000"><input type="text" id="realname" name="realname" value="{model:safe_show field='realname' /}" placeholder="请填写收货人姓名" class="fui-input"></div>
                    </div>
                    <div class="fui-cell">
                        <div class="fui-cell-label">联系电话</div>
                        <div class="fui-cell-info c000"><input type="tel" id="mobile" name="mobile" value="{model:safe_show field='mobile' /}" placeholder="请填写联系电话" class="fui-input"></div>
                    </div>
                    <div class="fui-cell">
                        <div class="fui-cell-label">所在地区</div>
                        <div class="fui-cell-info c000"><input type="text" id="areas" name="areas" data-value="" value="{model:safe_show field='areas' /}" placeholder="所在地区" class="fui-input" readonly="readonly"></div>
                    </div>
                    <div class="fui-cell">
                        <div class="fui-cell-label">详细地址</div>
                        <div class="fui-cell-info c000"><input type="text" id="address" name="address" value="{model:safe_show field='address' /}" placeholder="街道，楼牌号等" class="fui-input"></div>
                    </div>
                    {if(empty($model['is_default']))}
                    <div class="fui-cell ">
                        <div class="fui-cell-label" style="width:auto">设置默认地址</div>
                        <div class="fui-cell-info ">
                            <input type="checkbox" id="isdefault" class="fui-switch fui-switch-danger pull-right">
                        </div>
                    </div>
                    {/if}
                </div>
                <a id="btn-submit" class="external btn btn-danger block" style="margin-top:1.25rem">保存地址</a>
            </form>
        </div>
    </div>
</div>
{/block}

{block name="script"}
<script src="__STATIC__/js/jquery.gcjs.js"></script>
<script src="__STATIC__/m/foxui/js/foxui.picker.min.js"></script>
<script src="__STATIC__/m/foxui/js/foxui.citydata.min.js"></script>
<script>
    $(function () {
        $('#isdefault').on('click',function () {
            $(this).prop('checked')
        })
    })
    $('#areas').cityPicker({'title': '请选择所在城市'})
    $(document).on('click', '#btn-submit', function () {
        if ($(this).attr('submit')) {
            return
        }
        if ($('#realname').isEmpty()) {
            FoxUI.toast.show("请填写收件人");
            return
        }
        var jingwai = /(境外地区)+/.test($('#areas').val());
        if (jingwai) {
            if ($('#mobile').isEmpty()) {
                FoxUI.toast.show("请填写手机号码");
                return
            }
        } else {
            if (!$('#mobile').isMobile()) {
                FoxUI.toast.show("请填写正确手机号码");
                return
            }
        }
        if ($('#areas').isEmpty()) {
            FoxUI.toast.show("请填写所在地区");
            return
        }
        if ($('#address').isEmpty()) {
            FoxUI.toast.show("请填写详细地址");
            return
        }
        $('#btn-submit').html('正在处理...').attr('submit', 1);
        window.editAddressData = {
            realname: $('#realname').val(),
            mobile: $('#mobile').val(),
            address: $('#address').val(),
            areas: $('#areas').val(),
            isdefault: $('#isdefault').prop('checked') ? 1 : 0,
        };
        core.json("{:url('Member/address_submit')}", {
            id: $('#addressid').val(),
            addressdata: window.editAddressData
        }, function (json) {
            $('#btn-submit').html('保存地址').removeAttr('submit');
            if (json.code == 1) {
                window.editAddressData.id = json.data.addressid;
                FoxUI.toast.show('保存成功!');
                history.back()
            } else {
                FoxUI.toast.show(json.result.message)
            }
        }, true, true)
    })
    $('[data-toggle=delete]').unbind('click').click(function () {
        var id =  $(this).data('addressid');
        FoxUI.confirm('删除后无法恢复, 确认要删除吗 ?', function () {
            core.json("{:url('Member/address_delete')}", {id: id}, function (ret) {
                if (ret.code == 1) {
                    history.back();
                }
                FoxUI.toast.show(ret.msg)
            }, true, true)
        })
    });
</script>
{/block}